package models

import (
	"time"

	"github.com/shopspring/decimal"
)

// UserCard 用户卡表
type UserCard struct {
	ID                  int64           `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	UserID              int64           `gorm:"column:user_id;not null;index:idx_user_id" json:"user_id"`
	NikeName            string          `gorm:"column:nike_name;type:varchar(50)" json:"nike_name"`
	TotalTransferAmount decimal.Decimal `gorm:"column:TotalTransferAmount;type:decimal(20,6);not null;default:0" json:"totalTransferAmount"`
	CardID              string          `gorm:"column:card_id;type:varchar(50);uniqueIndex:uk_card_id;not null" json:"card_id"`
	Platform            string          `gorm:"column:platform;type:varchar(20);not null;index:idx_platform" json:"platform"`
	CardStatus          string          `gorm:"column:card_status;type:varchar(20);not null;index:idx_card_status" json:"card_status"`
	CardBin             string          `gorm:"column:card_bin;type:varchar(10);not null;default:'111111'" json:"card_bin"`
	CreatedAt           time.Time       `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt           time.Time       `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" json:"updated_at"`
	User                User            `gorm:"foreignKey:UserID;references:ID;constraint:fk_user_cards_user_id,OnUpdate:CASCADE" json:"user"`
	CardType            string          `gorm:"column:cardType;type:varchar(100);not null;default:'recharge'" json:"card_type"`
}

// TableName 获取表名
func (UserCard) TableName() string {
	return "user_cards"
}

// UserCardholder 用户持卡人模型
type UserCardholder struct {
	BaseModel
	UserID       int64  `json:"user_id" gorm:"not null;index:idx_user_id"`
	CardholderID string `json:"cardholder_id" gorm:"type:varchar(50);uniqueIndex:uk_cardholder_id;not null"`
	Platform     string `json:"platform" gorm:"type:varchar(20);not null;index:idx_platform"`
	FirstName    string `json:"first_name" gorm:"type:varchar(50)"`
	LastName     string `json:"last_name" gorm:"type:varchar(50)"`
	Email        string `json:"email" gorm:"type:varchar(100)"`
	Mobile       string `json:"mobile" gorm:"type:varchar(20);index:idx_mobile"`
	MobilePrefix string `json:"mobile_prefix" gorm:"type:varchar(10)"`
	Nationality  string `json:"nationality" gorm:"type:varchar(10)"`
	Status       string `json:"status" gorm:"type:varchar(20);default:normal;not null;index:idx_status"`

	// PhotonPay 持卡人信息
	BillingAddress          string `json:"billing_address" gorm:"type:varchar(255)"`      // 账单地址
	BillingCity             string `json:"billing_city" gorm:"type:varchar(50)"`          // 账单城市
	BillingCountry          string `json:"billing_country" gorm:"type:varchar(50)"`       // 账单国家
	BillingPostalCode       string `json:"billing_postal_code" gorm:"type:varchar(20)"`   // 账单邮编
	BillingState            string `json:"billing_state" gorm:"type:varchar(50)"`         // 账单州/省
	BillingAddressUpdatable bool   `json:"billing_address_updatable" gorm:"type:boolean"` // 账单地址是否可更新
}

// TableName 获取表名
func (UserCardholder) TableName() string {
	return "user_cardholder"
}

// CardFeeConfig 卡费配置模型
type CardFeeConfig struct {
	BaseModel
	CardType       string  `json:"card_type" gorm:"type:varchar(20);not null;index:idx_card_type"`
	CardFormFactor string  `json:"card_form_factor" gorm:"type:varchar(20);not null;index:idx_card_form_factor"`
	Currency       string  `json:"currency" gorm:"type:varchar(10);not null;index:idx_currency"`
	OpenCardFee    float64 `json:"open_card_fee" gorm:"type:decimal(10,2);not null"`
	MonthlyFee     float64 `json:"monthly_fee" gorm:"type:decimal(10,2);not null"`
	ExchangeRate   float64 `json:"exchange_rate" gorm:"type:decimal(10,6)"`
	RefundRatio    float64 `json:"refund_ratio" gorm:"type:decimal(5,2);default:0.00;not null"`
	Status         string  `json:"status" gorm:"type:varchar(20);default:normal;not null;index:idx_status"`
	CreatedBy      int64   `json:"created_by" gorm:"not null;index:idx_created_by"`
	UpdatedBy      int64   `json:"updated_by" gorm:"not null;index:idx_updated_by"`
}

// TableName 获取表名
func (CardFeeConfig) TableName() string {
	return "card_fee_configs"
}

// HlpayCardOpenRecord HlPay平台开卡记录表
// 对应表：hlpay_card_open_records
// 字段参考 database-design.sql
type HlpayCardOpenRecord struct {
	ID                int64           `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	UserID            int64           `gorm:"column:user_id;not null;index:idx_user_id" json:"user_id"`
	MatrixAccount     string          `gorm:"column:matrix_account;type:varchar(64);not null;index:idx_matrix_account" json:"matrix_account"`
	Platform          string          `gorm:"column:platform;type:varchar(32);not null;index:idx_platform" json:"platform"`
	CardID            string          `gorm:"column:card_id;type:varchar(50);not null;index:idx_card_id" json:"card_id"`
	CardScheme        string          `gorm:"column:card_scheme;type:varchar(20)" json:"card_scheme"`
	CardType          string          `gorm:"column:card_type;type:varchar(20)" json:"card_type"`
	CardFormFactor    string          `gorm:"column:card_form_factor;type:varchar(32)" json:"card_form_factor"`
	MaskCardNo        string          `gorm:"column:mask_card_no;type:varchar(32)" json:"mask_card_no"`
	MemberID          string          `gorm:"column:member_id;type:varchar(64)" json:"member_id"`
	Nickname          string          `gorm:"column:nickname;type:varchar(64)" json:"nickname"`
	CardNickname      string          `gorm:"column:card_nickname;type:varchar(64)" json:"card_nickname"`
	ReferenceNo       string          `gorm:"column:reference_no;type:varchar(64)" json:"reference_no"`
	FeeType           string          `gorm:"column:fee_type;type:varchar(32)" json:"fee_type"`
	ActualFeeAmount   decimal.Decimal `gorm:"column:actual_fee_amount;type:decimal(20,2)" json:"actual_fee_amount"`
	ActualFeeCurrency string          `gorm:"column:actual_fee_currency;type:varchar(10)" json:"actual_fee_currency"`
	Status            string          `gorm:"column:status;type:varchar(20);not null;default:'pending';index:idx_status" json:"status"`
	CreatedAt         time.Time       `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
}

func (HlpayCardOpenRecord) TableName() string {
	return "hlpay_card_open_records"
}

// BatchCardOpenRecord 批量开卡记录表
type BatchCardOpenRecord struct {
	ID             int64      `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	BatchID        string     `gorm:"column:batch_id;type:varchar(64);uniqueIndex:uk_batch_id;not null" json:"batch_id"`
	UserID         int64      `gorm:"column:user_id;not null;index:idx_user_id" json:"user_id"`
	MatrixAccount  string     `gorm:"column:matrix_account;type:varchar(64);not null;index:idx_matrix_account" json:"matrix_account"`
	Platform       string     `gorm:"column:platform;type:varchar(32);not null;index:idx_platform" json:"platform"`
	CardCount      int        `gorm:"column:card_count;not null" json:"card_count"`
	CardScheme     string     `gorm:"column:card_scheme;type:varchar(20)" json:"card_scheme"`
	CardType       string     `gorm:"column:card_type;type:varchar(20)" json:"card_type"`
	CardFormFactor string     `gorm:"column:card_form_factor;type:varchar(32)" json:"card_form_factor"`
	CardBin        string     `gorm:"column:card_bin;type:varchar(10)" json:"card_bin"`
	CardCurrency   string     `gorm:"column:card_currency;type:varchar(10)" json:"card_currency"`
	RechargeAmount string     `gorm:"column:recharge_amount;type:varchar(20)" json:"recharge_amount"`
	Cost           float64    `gorm:"column:cost;type:decimal(20,2)" json:"cost"`
	Status         string     `gorm:"column:status;type:varchar(20);not null;default:'processing';index:idx_status" json:"status"`
	SuccessCount   int        `gorm:"column:success_count;not null;default:0" json:"success_count"`
	FailedCount    int        `gorm:"column:failed_count;not null;default:0" json:"failed_count"`
	CreatedAt      time.Time  `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	CompletedAt    *time.Time `gorm:"column:completed_at" json:"completed_at"`
}

// TableName 获取表名
func (BatchCardOpenRecord) TableName() string {
	return "batch_card_open_records"
}
